package cn.ctbu.xiaopeng.studentscoremanage.service;

import cn.ctbu.xiaopeng.studentscoremanage.anno.Log;
import cn.ctbu.xiaopeng.studentscoremanage.dao.SCRepository;
import cn.ctbu.xiaopeng.studentscoremanage.exception.ResultException;
import cn.ctbu.xiaopeng.studentscoremanage.pojo.dto.ScoreQueryDTO;
import cn.ctbu.xiaopeng.studentscoremanage.pojo.entity.SC;
import cn.ctbu.xiaopeng.studentscoremanage.pojo.vo.ScorePageVO;
import cn.ctbu.xiaopeng.studentscoremanage.pojo.vo.page.TotalRow;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author xiaopeng
 * @version 1.0
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ScService {

    private final SCRepository scRepository;

    @Log
    public TotalRow getTotalRow() {
        TotalRow totalRow = scRepository.getTotalRow();
        Double totalScore = totalRow.getTotalScoreD();  // 假设这是 Double 类型
        Double avgGpa = totalRow.getAvgGpaD();  // 假设这是 Double 类型

        // 强制类型转换
        Integer totalScoreInt = (int) Math.floor(totalScore);  // 向下取整
        Integer avgGpaInt = (int) Math.floor(avgGpa);  // 向下取整

        // 创建新的 TotalRow 对象，传递转换后的值
        TotalRow resultTotalRow = new TotalRow(totalScoreInt, avgGpaInt);

        log.info("返回总分和平均绩点成功: {}", resultTotalRow);
        return resultTotalRow;
    }


    /**
     * 查询所有选课信息
     *
     * @return
     */
    @Log
    public List<SC> findAll() {
        return scRepository.findAll();
    }

    /**
     * 根据id查询选课信息
     *
     * @param id
     * @return
     */
    @Log
    public SC findById(Integer id) {
        return scRepository.findById(id).orElse(null); //orElse(null) 如果查询不到返回null
    }


    /**
     * 添加选课
     *
     * @param sc
     */
    @Log
    public SC insert(SC sc) {
        return scRepository.save(sc);
    }


    /**
     * 修改选课信息
     *
     * @param sc
     */
    @Log
    public SC update(SC sc) {
        return scRepository.save(sc);
    }

    /**
     * 删除选课
     *
     * @param id
     */
    @Log
    public void deleteById(Integer id) {
        scRepository.deleteById(id);
    }

    /**
     * 根据id批量删除选课
     *
     * @param ids
     */
    @Log
    @Transactional
    public void deleteSCByIds(List<Integer> ids) {
        if (ids != null && !ids.isEmpty()) {
            scRepository.deleteByIds(ids);
        } else {
            throw new ResultException("选课ID列表不能为空");
        }
    }

    /**
     * 分页查询
     *
     * @param pageable
     * @return
     */
    @Log
    public Page<SC> getAllWithPage(Pageable pageable) {
        return scRepository.findAll(pageable);
    }


    /**
     * 分页查询
     *
     * @param example
     * @param pageable
     * @return
     */
    @Log
    public Page<SC> getAllWithPage(Example<SC> example, Pageable pageable) {
        return scRepository.findAll(example, pageable);
    }

    /**
     * 根据条件查询选课信息
     *
     * @param scoreQueryDTO
     * @param pageable
     * @return
     */
    @Log
    public Page<ScorePageVO> getScorePageByConditions(ScoreQueryDTO scoreQueryDTO, Pageable pageable) {
        return scRepository.getScorePageByConditions(scoreQueryDTO, pageable);
    }

    /**
     * 根据条件查询选课信息
     * @param scoreQueryDTO
     * @return
     */
    public List<ScorePageVO> getAllSCPByConditions(ScoreQueryDTO scoreQueryDTO){
        return scRepository.getAllSCPByConditions(scoreQueryDTO);
    }


}
